
#include <iostream>
#include <memory>
#include <vector>
#include <stack>

// Step 1: 定义命令接口
class Command {
public:
    virtual ~Command() = default;

    // 执行命令
    virtual void execute() = 0;

    // 撤销命令
    virtual void undo() = 0;
};

// Step 2: 定义接收者 (灯光和音响)
class Light {
public:
    void turnOn() {
        std::cout << "The light is ON\n";
    }

    void turnOff() {
        std::cout << "The light is OFF\n";
    }
};

class Stereo {
public:
    void playMusic() {
        std::cout << "Stereo is playing music\n";
    }

    void stopMusic() {
        std::cout << "Stereo stopped playing music\n";
    }
};

// Step 3: 定义具体命令
class LightOnCommand : public Command {
private:
    Light& light_;

public:
    explicit LightOnCommand(Light& light) : light_(light) {}

    void execute() override {
        light_.turnOn();
    }

    void undo() override {
        light_.turnOff();
    }
};

class LightOffCommand : public Command {
private:
    Light& light_;

public:
    explicit LightOffCommand(Light& light) : light_(light) {}

    void execute() override {
        light_.turnOff();
    }

    void undo() override {
        light_.turnOn();
    }
};

class StereoPlayCommand : public Command {
private:
    Stereo& stereo_;

public:
    explicit StereoPlayCommand(Stereo& stereo) : stereo_(stereo) {}

    void execute() override {
        stereo_.playMusic();
    }

    void undo() override {
        stereo_.stopMusic();
    }
};

class StereoStopCommand : public Command {
private:
    Stereo& stereo_;

public:
    explicit StereoStopCommand(Stereo& stereo) : stereo_(stereo) {}

    void execute() override {
        stereo_.stopMusic();
    }

    void undo() override {
        stereo_.playMusic();
    }
};

// Step 4: 定义调用者
class RemoteControl {
private:
    std::shared_ptr<Command> onCommand_;
    std::shared_ptr<Command> offCommand_;
    std::stack<std::shared_ptr<Command>> commandHistory_; // 用于撤销

public:
    void setCommands(std::shared_ptr<Command> onCommand, std::shared_ptr<Command> offCommand) {
        onCommand_ = onCommand;
        offCommand_ = offCommand;
    }

    void pressOnButton() {
        if (onCommand_) {
            onCommand_->execute();
            commandHistory_.push(onCommand_);
        }
    }

    void pressOffButton() {
        if (offCommand_) {
            offCommand_->execute();
            commandHistory_.push(offCommand_);
        }
    }

    void pressUndoButton() {
        if (!commandHistory_.empty()) {
            auto lastCommand = commandHistory_.top();
            commandHistory_.pop();
            lastCommand->undo();
        } else {
            std::cout << "No commands to undo\n";
        }
    }
};

// Step 5: 客户端代码
int main() {
    // 创建接收者
    Light livingRoomLight;
    Stereo homeStereo;

    // 创建命令
    auto lightOn = std::make_shared<LightOnCommand>(livingRoomLight);
    auto lightOff = std::make_shared<LightOffCommand>(livingRoomLight);
    auto stereoPlay = std::make_shared<StereoPlayCommand>(homeStereo);
    auto stereoStop = std::make_shared<StereoStopCommand>(homeStereo);

    // 配置遥控器
    RemoteControl remote;
    remote.setCommands(lightOn, lightOff);

    // 控制灯光
    std::cout << "Testing Light Commands:\n";
    remote.pressOnButton();
    remote.pressOffButton();
    remote.pressUndoButton();

    // 切换到音响控制
    remote.setCommands(stereoPlay, stereoStop);

    // 控制音响
    std::cout << "\nTesting Stereo Commands:\n";
    remote.pressOnButton();
    remote.pressOffButton();
    remote.pressUndoButton();

    return 0;
}


